package ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;

import utils.KeyGen;

public class KeyGeneratorDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField keyLengthtextField;
	private JTextField algoTextField;
	private JTextPane privateKeytextPane;
	private JTextPane publicKeyTextPane;

	/**
	 * Create the dialog.
	 */
	public KeyGeneratorDialog() {
		setTitle("Pair Key Generator");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.NORTH);
			panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
			{
				JLabel lblKeyLength = new JLabel("Key Length :");
				panel.add(lblKeyLength);
			}
			{
				keyLengthtextField = new JTextField();
				keyLengthtextField.setText("2048");
				panel.add(keyLengthtextField);
				keyLengthtextField.setColumns(10);
			}
			{
				JLabel lblKeyAlgorithm = new JLabel("Key algorithm :");
				panel.add(lblKeyAlgorithm);
			}
			{
				algoTextField = new JTextField();
				algoTextField.setText("RSA");
				panel.add(algoTextField);
				algoTextField.setColumns(10);
			}
			{
				JButton btnGenerate = new JButton("Generate");
				btnGenerate.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						try {
							int keyLength = Integer.parseInt(keyLengthtextField
									.getText());
							String algo = algoTextField.getText();
							KeyGen.generateKeyPair(keyLength, algo);
							privateKeytextPane.setText(KeyGen.keyPair
									.getPrivate().toString());
							publicKeyTextPane.setText(KeyGen.keyPair
									.getPublic().toString());
							
							JOptionPane.showMessageDialog(
									KeyGeneratorDialog.this,"Key Pair generated.");
							
						} catch (Exception d) {
							JOptionPane.showInternalMessageDialog(
									KeyGeneratorDialog.this, d.toString());
						}
					}
				});
				panel.add(btnGenerate);
			}
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.CENTER);
			panel.setLayout(new GridLayout(0, 1, 0, 0));
			{
				JPanel panel_1 = new JPanel();
				panel.add(panel_1);
				panel_1.setLayout(new BorderLayout(0, 0));
				{
					JLabel lblPrivateKey = new JLabel("Private Key :");
					panel_1.add(lblPrivateKey, BorderLayout.NORTH);
				}
				{
					JScrollPane scrollPane = new JScrollPane();
					panel_1.add(scrollPane, BorderLayout.CENTER);
					{
						privateKeytextPane = new JTextPane();
						scrollPane.setViewportView(privateKeytextPane);
					}
				}
			}
			{
				JPanel panel_1 = new JPanel();
				panel.add(panel_1);
				panel_1.setLayout(new BorderLayout(0, 0));
				{
					JLabel lblPublicKey = new JLabel("Public Key :");
					panel_1.add(lblPublicKey, BorderLayout.NORTH);
				}
				{
					JScrollPane scrollPane = new JScrollPane();
					panel_1.add(scrollPane, BorderLayout.CENTER);
					{
						publicKeyTextPane = new JTextPane();
						scrollPane.setViewportView(publicKeyTextPane);
					}
				}
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Save...");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						JFileChooser chooser = new JFileChooser();
						chooser.setCurrentDirectory(new java.io.File("."));
						chooser.setDialogTitle("");
						chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
						chooser.setAcceptAllFileFilterUsed(false);
						//
						if (chooser.showSaveDialog(KeyGeneratorDialog.this) == JFileChooser.APPROVE_OPTION) {
							try {
								KeyGen.SaveKeyPair(chooser.getSelectedFile()
										.getPath(), KeyGen.keyPair);
								
								JOptionPane.showMessageDialog(
										KeyGeneratorDialog.this,"Key Pair saved.");
								
							} catch (IOException e1) {
								JOptionPane.showMessageDialog(
										KeyGeneratorDialog.this, e1.toString());
							}
						}
					}

					// KeyGen.SaveKeyPair(path, keyPair)
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Quit");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						KeyGeneratorDialog.this.dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}

		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
}
